package cn.edu.njupt.post.controller;

import cn.edu.njupt.post.async.EventModel;
import cn.edu.njupt.post.async.EventProducer;
import cn.edu.njupt.post.async.EventType;
import cn.edu.njupt.post.model.*;
import cn.edu.njupt.post.service.CommentService;
import cn.edu.njupt.post.service.LikeService;
import cn.edu.njupt.post.service.PostService;
import cn.edu.njupt.post.service.UserService;
import cn.edu.njupt.post.util.JedisAdapter;
import cn.edu.njupt.post.util.OtherUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping({"/post"})
public class PostController {
    private static final Logger logger = LoggerFactory.getLogger(JedisAdapter.class);

    @Autowired
    PostService postService;

    @Autowired
    CommentService commentService;

    @Autowired
    HostHolder hostHolder;

    @Autowired
    UserService userService;

    @Autowired
    LikeService likeService;

    @Autowired
    EventProducer eventProducer;

    @RequestMapping(value = "/{postId}", method = {RequestMethod.GET})
    public String postDetail(Model model, @PathVariable("postId") int postId) {
        Post post = postService.getById(postId);
        model.addAttribute("post", post);
        User user = userService.getUser(post.getUserId());
        model.addAttribute("thisUser", user);
        List<Comment> commentList = commentService.getCommentsByPost(postId);
        List<ViewObject> vos = new ArrayList<>();
        for (Comment comment : commentList) {
            ViewObject vo = new ViewObject();
            if (comment.getEntityType() == 2) {
                User replyUser = userService.getUser(commentService.getComment(comment.getEntityId()).getUserId());
                vo.set("replyUser", replyUser);
            }
            vo.set("comment", comment);
            vo.set("user", userService.getUser(comment.getUserId()));
            vos.add(vo);
        }
        model.addAttribute("comments", vos);
        //model.addAttribute("commentCnt", commentList.size());


        if (hostHolder.getUser() == null) {
            model.addAttribute("liked", 0);
        } else {
            model.addAttribute("liked", likeService.getLikeStatus(hostHolder.getUser().getId(), EntityType.ENTITY_POST, post.getId()));
        }

        model.addAttribute("likeCount", likeService.getLikeCount(EntityType.ENTITY_POST, post.getId()));

        return "detail";
    }

    @RequestMapping(value = "/add", method = {RequestMethod.POST})

    public String addPost(@RequestParam("title") String title, @RequestParam("content") String content) {
        try {
            Post post = new Post();
            post.setContent(content)
                    .setCreatedDate(new Date())
                    .setTitle(title);
            if (hostHolder.getUser() == null) {
                //gg
                post.setUserId(OtherUtil.ANONYMOUS_USERID);
                // return WendaUtil.getJSONString(999);
            } else {
                post.setUserId(hostHolder.getUser().getId());
                postService.addPost(post);

//                    return OtherUtil.getJSONString(0);

            }

        } catch (Exception e) {
            logger.error("增加题目失败" + e.getMessage());
        }
//        return OtherUtil.getJSONString(1, "失败");
        return "redirect:/";
    }

    @RequestMapping(value = "/remove", method = {RequestMethod.POST})
    @ResponseBody
    public String removePost(@RequestParam("id") int id) {
        postService.deleteById(id);
        return OtherUtil.getJSONString(0, "ok");
    }
}
